/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Aplicacion.java
 *
 * Created on 05/06/2011, 09:14:09 PM
 */

package aplicacion;

import java.sql.ResultSet;
import javax.swing.JOptionPane;

/**
 *
 * @author diego
 */
public final class Aplicacion extends javax.swing.JFrame {

    /** Creates new form Aplicacion */
    public Aplicacion() {
        initComponents();
        configurarFormulario();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar1 = new javax.swing.JToolBar();
        jButtonNuevo = new javax.swing.JButton();
        jButtonBorrar = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jButtonRefrescar = new javax.swing.JButton();
        jButtonJoin = new javax.swing.JButton();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanelTablas = new javax.swing.JPanel();
        jPanelTabla1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jPanelTabla3 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTable3 = new javax.swing.JTable();
        jPanelTabla2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jPanelTablasCruzadas = new javax.swing.JPanel();
        jPanelTabla__3 = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        jTable4 = new javax.swing.JTable();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Aplicacion Matematica Discreta");
        setResizable(false);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jToolBar1.setFloatable(false);

        jButtonNuevo.setText("NUEVO");
        jButtonNuevo.setFocusable(false);
        jButtonNuevo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButtonNuevo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButtonNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonNuevoActionPerformed(evt);
            }
        });
        jToolBar1.add(jButtonNuevo);

        jButtonBorrar.setText("BORRAR");
        jButtonBorrar.setEnabled(false);
        jButtonBorrar.setFocusable(false);
        jButtonBorrar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButtonBorrar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jButtonBorrar);

        jButton1.setText("MODIFICAR");
        jButton1.setEnabled(false);
        jButton1.setFocusable(false);
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jButton1);

        jButtonRefrescar.setText("REFRESCAR");
        jButtonRefrescar.setFocusable(false);
        jButtonRefrescar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButtonRefrescar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButtonRefrescar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRefrescarActionPerformed(evt);
            }
        });
        jToolBar1.add(jButtonRefrescar);

        jButtonJoin.setText("CRUZAR");
        jButtonJoin.setFocusable(false);
        jButtonJoin.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButtonJoin.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButtonJoin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonJoinActionPerformed(evt);
            }
        });
        jToolBar1.add(jButtonJoin);

        getContentPane().add(jToolBar1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 1040, 40));

        jPanelTablas.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanelTabla1.setBorder(javax.swing.BorderFactory.createTitledBorder("Movistar Cablemagico"));
        jPanelTabla1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jTable1.setAutoCreateRowSorter(true);
        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "DNI", "Nombres", "Apellido Paterno", "Apellido Materno", "Sexo"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, true, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(jTable1);

        jPanelTabla1.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 20, 310, 320));

        jPanelTablas.add(jPanelTabla1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, 330, 350));

        jPanelTabla3.setBorder(javax.swing.BorderFactory.createTitledBorder("Movistar Internet"));
        jPanelTabla3.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jTable3.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "DNI", "Nombres", "Apellido Paterno", "Apellido Materno", "Sexo"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, true, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane3.setViewportView(jTable3);

        jPanelTabla3.add(jScrollPane3, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 20, 310, 320));

        jPanelTablas.add(jPanelTabla3, new org.netbeans.lib.awtextra.AbsoluteConstraints(670, 10, 330, 350));
        jPanelTabla3.getAccessibleContext().setAccessibleName("Tabla 3");

        jPanelTabla2.setBorder(javax.swing.BorderFactory.createTitledBorder("Movistar Telefono"));
        jPanelTabla2.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "DNI", "Nombres", "Apellido Paterno", "Apellido Materno", "Sexo"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, true, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(jTable2);

        jPanelTabla2.add(jScrollPane2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 20, 310, 320));

        jPanelTablas.add(jPanelTabla2, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 10, 330, 350));

        jTabbedPane1.addTab("Tablas Sin Cruzar", jPanelTablas);

        jPanelTablasCruzadas.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanelTabla__3.setBorder(javax.swing.BorderFactory.createTitledBorder("Joins Movistar Cablemagico, Movistar Telefono, Movistar Internet"));
        jPanelTabla__3.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jTable4.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "DNI", "Nombres", "Apellido Paterno", "Apellido Materno", "Sexo"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, true, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane4.setViewportView(jTable4);

        jPanelTabla__3.add(jScrollPane4, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 20, 620, 320));

        jPanelTablasCruzadas.add(jPanelTabla__3, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, 660, 340));

        jTabbedPane1.addTab("Tablas Cruzadas", jPanelTablasCruzadas);

        getContentPane().add(jTabbedPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 60, 1020, 410));

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/aplicacion/MOVISTAR_.jpg"))); // NOI18N
        getContentPane().add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(960, 40, 50, 40));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonRefrescarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRefrescarActionPerformed
        configurarFormulario();
    }//GEN-LAST:event_jButtonRefrescarActionPerformed

    private void jButtonJoinActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonJoinActionPerformed
        generarTablaJoin();
        cargarTablaJoin();
    }//GEN-LAST:event_jButtonJoinActionPerformed

    private void jButtonNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonNuevoActionPerformed
        Nuevo ventana_nuevo = new Nuevo(this, true);
        ventana_nuevo.setVisible(true);
        // TODO add your handling code here:
    }//GEN-LAST:event_jButtonNuevoActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                
                new Aplicacion().setVisible(true);

            }
        });
    }

    public void cargarTablaJoin()
    {
        ResultSet rs  = null;
        ConexionMySQL con = new ConexionMySQL();
        if (con.verificarConexion() != null)
        {
            con.abrirConexion();
            rs = con.consultarRegistros(" MOVISTAR_JOIN ", " * ", "");
            ingresarTabla(4,rs);
        }
        else
        {
            JOptionPane.showMessageDialog(this,"No se pudo establecer conexion con la BD MOVISTAR, \n "
                                        + "asegurese de que el servidor de BD este ejecutandose o \n "
                                        + "que la BD fue cargada correctamente.");
        }
    }

    public void configurarFormulario()
    {
        ResultSet rs  = null;
        ConexionMySQL con = new ConexionMySQL();
        if (con.verificarConexion() != null)
        {
            con.abrirConexion();
            rs = con.consultarRegistros(" MOVISTAR_CABLEMAGICO ", " * ", "");
            ingresarTabla(1,rs);
            rs = con.consultarRegistros(" MOVISTAR_TELEFONO ", " * ", "");
            ingresarTabla(2,rs);
            rs = con.consultarRegistros(" MOVISTAR_INTERNET ", " * ", "");
            ingresarTabla(3,rs);
            con.cerrarConexion();
        }
        else
        {
            JOptionPane.showMessageDialog(this,"No se pudo establecer conexion con la BD MOVISTAR, \n "
                                        + "asegurese de que el servidor de BD este ejecutandose o \n "
                                        + "que la BD fue cargada correctamente.");
        }
        
    }

    public void generarTablaJoin()
    {
       ResultSet rs  = null;
        ConexionMySQL con = new ConexionMySQL();
        if (con.verificarConexion() != null)
        {
            con.abrirConexion();
            con.eliminarTabla();
            con.crearTabla();
            con.insertarTablas(" MOVISTAR_JOIN ", ""," SELECT MC.movi_cable_dni "
                             + " ,MC.movi_cable_nombres "
                             + " ,MC.movi_cable_apellido_paterno "
                             + " ,MC.movi_cable_apellido_materno "
                             + " ,MC.movi_cable_sexo"
                             + " FROM MOVISTAR_CABLEMAGICO MC, MOVISTAR_TELEFONO MT, MOVISTAR_INTERNET MI "
                             + " WHERE MC.movi_cable_dni = MT.movi_tel_dni and MC.movi_cable_dni = MI.movi_inter_dni ",  "");

            con.cerrarConexion();
        }
        else
        {
            JOptionPane.showMessageDialog(this,"No se pudo establecer conexion con la BD MOVISTAR, \n "
                                        + "asegurese de que el servidor de BD este ejecutandose o \n "
                                        + "que la BD fue cargada correctamente.");
        }
    }

    public void ingresarTabla(int nombre_tabla, ResultSet rs)
    {
        int i = 0;
        try
        {
            while (rs.next())
            {
                switch(nombre_tabla)
                {
                    case 1:
                            jTable1.setValueAt(rs.getString(1), i , 0);
                            jTable1.setValueAt(rs.getString(2), i , 1);
                            jTable1.setValueAt(rs.getString(3), i , 2);
                            jTable1.setValueAt(rs.getString(4), i , 3);
                            jTable1.setValueAt(rs.getString(5), i , 4);
                    break;
                    case 2:
                            jTable2.setValueAt(rs.getString(1), i , 0);
                            jTable2.setValueAt(rs.getString(2), i , 1);
                            jTable2.setValueAt(rs.getString(3), i , 2);
                            jTable2.setValueAt(rs.getString(4), i , 3);
                            jTable2.setValueAt(rs.getString(5), i , 4);
                    break;
                    case 3:
                            jTable3.setValueAt(rs.getString(1), i , 0);
                            jTable3.setValueAt(rs.getString(2), i , 1);
                            jTable3.setValueAt(rs.getString(3), i , 2);
                            jTable3.setValueAt(rs.getString(4), i , 3);
                            jTable3.setValueAt(rs.getString(5), i , 4);
                    break;
                    case 4:
                            jTable4.setValueAt(rs.getString(1), i , 0);
                            jTable4.setValueAt(rs.getString(2), i , 1);
                            jTable4.setValueAt(rs.getString(3), i , 2);
                            jTable4.setValueAt(rs.getString(4), i , 3);
                            jTable4.setValueAt(rs.getString(5), i , 4);
                    break;
                }
                
                i++;
            }
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
        
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButtonBorrar;
    private javax.swing.JButton jButtonJoin;
    private javax.swing.JButton jButtonNuevo;
    private javax.swing.JButton jButtonRefrescar;
    private javax.swing.JPanel jPanelTabla1;
    private javax.swing.JPanel jPanelTabla2;
    private javax.swing.JPanel jPanelTabla3;
    private javax.swing.JPanel jPanelTabla__3;
    private javax.swing.JPanel jPanelTablas;
    private javax.swing.JPanel jPanelTablasCruzadas;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    private javax.swing.JTable jTable3;
    private javax.swing.JTable jTable4;
    private javax.swing.JToolBar jToolBar1;
    // End of variables declaration//GEN-END:variables

}
